//
//  XGL_PersonalCommentTableViewCell.swift
//  XGamesLive
//
//  Created by ios on 2024/5/21.
//

import UIKit

class XGL_PersonalCommentTableViewCell: UITableViewCell {
    
    var itemHeaderView: UserHeadView!
    var itemNameText: UILabel!
    var itemContentText: UILabel!
    var itemDateText: UILabel!
    var itemThumbButton: MXCustomButton!
    
    var itemData: PublishCommentData = PublishCommentData(){
        didSet{
            if itemData.listActionType.contains(2){
                self.itemThumbButton.isSelected = true
            }else{
                self.itemThumbButton.isSelected = false
            }
            if let thumbDic = itemData.listActionCount.first(where: {$0.type == 2}) {
                self.thumbCount = thumbDic.cnt
            }
            self.itemContentText.text = self.itemData.data8
            if let date = itemData.createTime.stringToDate() {
                self.itemDateText.text = date.toFormat("yyyy.MM.dd HH:mm")
            }
            if let userData = itemData.auditUserExt {
                self.itemHeaderView.userData = userData
                self.itemNameText.text = userData.data1
            }
        }
    }
    var thumbCount: NSInteger = 0{
        didSet{
            if thumbCount > 0 {
                self.itemThumbButton.setTitle(thumbCount.description, for: .normal)
            }else{
                self.itemThumbButton.setTitle("0", for: .normal)
            }
            self.itemThumbButton.imagePosition(at: .left, space: 6.6)
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.contentView.backgroundColor = .clear
        self.backgroundColor = .clear
        self.selectionStyle = .none
        self.createXglView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension XGL_PersonalCommentTableViewCell{
    private func createXglView(){
        self.itemHeaderView = UserHeadView().then({ make in
            make.layerCornerRadius = 4.0
            make.clipsToBounds = true
        })
        self.itemNameText = UILabel().then({ make in
            make.textColor = UIColor.white
            make.font = UIFont.pingfangSC_Regular(fontSize: 16.0)
        })
        self.itemContentText = UILabel().then({ make in
            make.textColor = UIColor.white
            make.font = UIFont.pingfangSC_Regular(fontSize: 14.0)
            make.numberOfLines = 0
            make.lineBreakMode = .byWordWrapping
        })
        self.itemDateText = UILabel().then({ make in
            make.textColor = UIColor.hexColor("#A2A2A2")
            make.font = UIFont.pingfangSC_Regular(fontSize: 12.0)
        })
        self.itemThumbButton = MXCustomButton().then({ make in
            make.setImage(imageWithName("icon_thumb_normal"), for: .normal)
            make.setImage(imageWithName("icon_thumb_selected"), for: .selected)
            make.setTitle("0", for: .normal)
            make.setTitleColor(UIColor.white, for: .normal)
            make.titleLabel?.font = UIFont.pingfangSC_Regular(fontSize: 12.0)
            make.imagePosition(at: .left, space: 6.6)
        })
        let line = UIView().then { make in
            make.backgroundColor = UIColor.hexColor("#A2A2A2").withAlphaComponent(0.2)
        }
        self.contentView.addSubviews([itemHeaderView,itemNameText,itemContentText,itemDateText,itemThumbButton,line])
        
        itemHeaderView.snp.makeConstraints { make in
            make.left.top.equalTo(16.0)
            make.width.height.equalTo(40.0)
        }
        itemNameText.snp.makeConstraints { make in
            make.left.equalTo(itemHeaderView.snp.right).offset(12.0)
            make.top.equalTo(itemHeaderView)
        }
        itemContentText.snp.makeConstraints { make in
            make.left.equalTo(itemNameText)
            make.top.equalTo(itemNameText.snp.bottom).offset(6.0)
            make.right.equalTo(-16.0)
        }
        itemDateText.snp.makeConstraints { make in
            make.left.equalTo(itemNameText)
            make.top.equalTo(itemContentText.snp.bottom).offset(5.0)
        }
        itemThumbButton.snp.makeConstraints { make in
            make.right.equalTo(-10.0)
            make.centerY.equalTo(itemNameText)
            make.height.equalTo(22.0)
        }
        line.snp.makeConstraints { make in
            make.centerX.bottom.equalToSuperview()
            make.height.equalTo(1.0).priority(.high)
            make.left.equalTo(16.0)
            make.top.equalTo(itemDateText.snp.bottom).offset(16.0)
        }
    }
}
